#include "modbuswig.h"
#include "ui_modbuswig.h"

ModbusWig::ModbusWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWig)
{
    ui->setupUi(this);
    //设置一个标志位作为上报类型
    connect(ui->pushButton_2_flash,SIGNAL(clicked()),this,SLOT(dataDownSlot()));
    connect(ui->pushButton_5_light,SIGNAL(clicked()),this,SLOT(lightdataDownSlot()));
    connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(lightdataDownSlot()));


}

ModbusWig::~ModbusWig()
{
    delete ui;
}

void ModbusWig::dataUpSlot(QByteArray buf)
{
    //    qDebug()<<buf;
    QString jsonString=buf;
    //         QString jsonString =
    //                 R"({
    //                    "type": 1,
    //                    "result": 0,
    //                    "data": [{
    //                            "key": 101,
    //                            "val": "22.5"
    //                        },
    //                        {
    //                            "key": 102,
    //                            "val": "52.5"
    //                        },
    //                        {
    //                            "key": 103,
    //                            "val": "2"
    //                        }
    //                    ]}
    //                     )";

    QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8());

    // 检查解析是否成功
    if (!doc.isNull() && doc.isObject()) {
        QJsonObject jsonObj = doc.object();//创建对象

        // 读取JSON对象中的值
        int type = jsonObj["type"].toInt();
        bool result = jsonObj["result"].toBool();




        QJsonArray data = jsonObj["data"].toArray();

        //获取数组个数
        int n=data.size();
        qDebug()<<n;

        // 遍历数组并输出每个对象的内容
        for (const QJsonValue &value : data) {
            QJsonObject obj = value.toObject();
            int key = obj["key"].toInt();
            QString val = obj["val"].toString();
                  //  qDebug() << "Key:" << key << ", Val:" << val;

            if(key==101)
            {QString temtext("光照：");
                temtext.append(val);
                temtext.append("");
                ui->pushButton_4_illumination->setText(temtext);
            }
            if(key==102&&light==0)
            {QString temtext("灯光：");

                if(val.toInt()==1)
                {
                    temtext.append("开");
                    light=1;
                }
                if(val.toInt()==0)
                {
                    temtext.append("关");
                    light=1;
                }
                ui->pushButton_5_light->setText(temtext);
            }
            if(key==103)
            {QString temtext("烟雾报警：");
                temtext.append(val);
                temtext.append("");
                ui->pushButton_6_mog->setText(temtext);
            }
            if(key==104)
            {
                QString temtext("报警：");
                if(val.toInt()==1)
                {  temtext.append("开");
                }
                else
                {temtext.append("关");
                }

                ui->pushButton_8->setText(temtext);

            }
        }


    } else {
        qDebug() << "Failed to parse JSON.";
    }



}

void ModbusWig::dataDownSlot()
{
    QJsonObject address;
    address.insert("type",1);
    address.insert("limit",1);
    address.insert("key",1);
    //    address["type"] = 1;
    //    address["limit"] = "all";
    //    address["key"] = 1;
    // 将QJsonObject转换为QJsonDocument
    QJsonDocument jsonDoc(address);

    // 输出JSON字符串
    QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
    qDebug() << "Serialized JSON:" << jsonString;

    // 创建一个QMQTT::Message对象
    quint16 idd = 1;
    QString topic = "/app/data/down";
    QByteArray payload = jsonString.toUtf8();
    //    QByteArray payload = jsonDoc.toBinaryData();
    quint8 qos = 1;
    bool retain = false;
    QMQTT::Message message(idd, topic, payload, qos, retain);
    GlobalVal::mqtt->publish( message);

}

void ModbusWig::lightdataDownSlot()
{
    QJsonObject address;
    address["type"] = 2;
    QJsonObject data;
    if(ui->pushButton_5_light== sender())
    {if(flag1_light==1)
        {
            data["key"]=102;
            data["val"]="0";
          flag1_light=0;
            QString temtext("灯光：");
            temtext.append("关");
            ui->pushButton_5_light->setText(temtext);

        }else if(flag1_light==0)
        { data["key"]=102;
            data["val"]="1";
            QString temtext("灯光：");
            temtext.append("开");
           ui->pushButton_5_light->setText(temtext);
            flag1_light=1;

        }

    }else if(ui->pushButton_8 == sender())
    {
        if(flag2_siren==1)
                {
                    data["key"]=104;
                    data["val"]="0";
                 flag2_siren=0;
                    QString temtext("报警：");
                    temtext.append("关");
                   ui->pushButton_8->setText(temtext);

                }else if(flag2_siren==0)
                { data["key"]=104;
                    data["val"]="1";
                    QString temtext("报警：");
                    temtext.append("开");
                   ui->pushButton_8->setText(temtext);
                   flag2_siren=1;

                }
    }

 address.insert("data",QJsonValue(data));



    // 将QJsonObject转换为QJsonDocument
    QJsonDocument jsonDoc(address);

    // 输出JSON字符串
    QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
    qDebug() << "Serialized JSON:" << jsonString;

    // 创建一个QMQTT::Message对象
    quint16 idd = 1;
    QString topic = "/app/data/down";
    QByteArray payload = jsonString.toUtf8();
    //    QByteArray payload = jsonDoc.toBinaryData();
    quint8 qos = 1;
    bool retain = false;
    QMQTT::Message message(idd, topic, payload, qos, retain);
    GlobalVal::mqtt->publish( message);

}
